
#include "MyEGLView.h"
#include "cocoa/CCSet.h"
#include "ccMacros.h"
#include "CCDirector.h"
#include "touch_dispatcher/CCTouch.h"
#include "touch_dispatcher/CCTouchDispatcher.h"
#include "text_input_node/CCIMEDispatcher.h"
#include "keypad_dispatcher/CCKeypadDispatcher.h"
#include "support/CCPointExtension.h"
#include "CCApplication.h"


//////////////////////////////////////////////////////////////////////////
// impliment MyEGLView
//////////////////////////////////////////////////////////////////////////
static MyEGLView* s_pMainWindow = NULL;
static const WCHAR* kWindowClassName = L"Cocos2dxWin32";

MyEGLView::MyEGLView()
	: m_bCaptured(false)
	, m_hWnd(NULL)
	, m_hDC(NULL)
	, m_hRC(NULL)
{
	strcpy(m_szViewName, "Cocos2dxWin32");
}

MyEGLView::~MyEGLView()
{

}

void MyEGLView::init(HWND hWnd, HDC  hDC, HGLRC hRC)
{
	m_hWnd = hWnd;
	m_hDC = hDC;
	m_hRC = hRC;
}

LRESULT MyEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// 	switch (message)
// 	{
// 	case WM_LBUTTONDOWN:
// 		if (m_pDelegate && MK_LBUTTON == wParam)
// 		{
// 			POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};
// 			CCPoint pt(point.x/CC_CONTENT_SCALE_FACTOR(), point.y/CC_CONTENT_SCALE_FACTOR());
// 			CCPoint tmp = ccp(pt.x, m_obScreenSize.height - pt.y);
// 			if (m_obViewPortRect.equals(CCRectZero) || m_obViewPortRect.containsPoint(tmp))
// 			{
// 				m_bCaptured = true;
// 				SetCapture(m_hWnd);
// 				int id = 0;
// 				handleTouchesBegin(1, &id, &pt.x, &pt.y);
// 			}
// 		}
// 		break;
// 
// 	case WM_MOUSEMOVE:
// 		if (MK_LBUTTON == wParam && m_bCaptured)
// 		{
// 			POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};
// 			CCPoint pt(point.x/CC_CONTENT_SCALE_FACTOR(), point.y/CC_CONTENT_SCALE_FACTOR());
// 			int id = 0;
// 			handleTouchesMove(1, &id, &pt.x, &pt.y);
// 		}
// 		break;
// 
// 	case WM_LBUTTONUP:
// 		if (m_bCaptured)
// 		{
// 			POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};
// 			CCPoint pt(point.x/CC_CONTENT_SCALE_FACTOR(), point.y/CC_CONTENT_SCALE_FACTOR());
// 			int id = 0;
// 			handleTouchesEnd(1, &id, &pt.x, &pt.y);
// 
// 			ReleaseCapture();
// 			m_bCaptured = false;
// 		}
// 		break;
// 	case WM_SIZE:
// 		switch (wParam)
// 		{
// 		case SIZE_RESTORED:
// 			CCApplication::sharedApplication()->applicationWillEnterForeground();
// 			break;
// 		case SIZE_MINIMIZED:
// 			CCApplication::sharedApplication()->applicationDidEnterBackground();
// 			break;
// 		}
// 		break;
// 	case WM_KEYDOWN:
// 		if (wParam == VK_F1 || wParam == VK_F2)
// 		{
// 			CCDirector* pDirector = CCDirector::sharedDirector();
// 			if (GetKeyState(VK_LSHIFT) < 0 ||  GetKeyState(VK_RSHIFT) < 0 || GetKeyState(VK_SHIFT) < 0)
// 				pDirector->getKeypadDispatcher()->dispatchKeypadMSG(wParam == VK_F1 ? kTypeBackClicked : kTypeMenuClicked);
// 		}
// 		if ( m_lpfnAccelerometerKeyHook!=NULL )
// 		{
// 			(*m_lpfnAccelerometerKeyHook)( message,wParam,lParam );
// 		}
// 		break;
// 	case WM_KEYUP:
// 		if ( m_lpfnAccelerometerKeyHook!=NULL )
// 		{
// 			(*m_lpfnAccelerometerKeyHook)( message,wParam,lParam );
// 		}
// 		break;
// 	case WM_CHAR:
// 		{
// 			if (wParam < 0x20)
// 			{
// 				if (VK_BACK == wParam)
// 				{
// 					CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward();
// 				}
// 				else if (VK_RETURN == wParam)
// 				{
// 					CCIMEDispatcher::sharedDispatcher()->dispatchInsertText("\n", 1);
// 				}
// 				else if (VK_TAB == wParam)
// 				{
// 					// tab input
// 				}
// 				else if (VK_ESCAPE == wParam)
// 				{
// 					// ESC input
// 					CCDirector::sharedDirector()->end();
// 				}
// 			}
// 			else if (wParam < 128)
// 			{
// 				// ascii char
// 				CCIMEDispatcher::sharedDispatcher()->dispatchInsertText((const char *)&wParam, 1);
// 			}
// 			else
// 			{
// 				char szUtf8[8] = {0};
// 				int nLen = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)&wParam, 1, szUtf8, sizeof(szUtf8), NULL, NULL);
// 				CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(szUtf8, nLen);
// 			}
// 			if ( m_lpfnAccelerometerKeyHook!=NULL )
// 			{
// 				(*m_lpfnAccelerometerKeyHook)( message,wParam,lParam );
// 			}
// 		}
// 		break;
// 	case WM_PAINT:
// 		PAINTSTRUCT ps;
// 		BeginPaint(m_hWnd, &ps);
// 		EndPaint(m_hWnd, &ps);
// 		break;
// 
// 	case WM_CLOSE:
// 		CCDirector::sharedDirector()->end();
// 		break;
// 
// 	case WM_DESTROY:
// 		destroyGL();
// 		PostQuitMessage(0);
// 		break;
// 
// 	default:
// 		return DefWindowProc(m_hWnd, message, wParam, lParam);
// 	}
// 	return 0;

	return DefWindowProc(m_hWnd, message, wParam, lParam);
}


bool MyEGLView::isOpenGLReady()
{
	return (m_hDC != NULL && m_hRC != NULL);
}

void MyEGLView::end()
{
	delete this;
}

void MyEGLView::swapBuffers()
{
	if (m_hDC != NULL)
	{
		::SwapBuffers(m_hDC);
	}
}


void MyEGLView::setIMEKeyboardState(bool /*bOpen*/)
{

}

bool MyEGLView::enableRetina()
{
	m_bIsRetinaEnabled = true;
	return true;
}

HWND MyEGLView::getHWnd()
{
	return m_hWnd;
}

void MyEGLView::setFrameSize(float width, float height)
{
	CCEGLViewProtocol::setFrameSize(width, height);
}

bool MyEGLView::setContentScaleFactor(float contentScaleFactor)
{
	CCEGLViewProtocol::setContentScaleFactor(contentScaleFactor);

	return true;
}

MyEGLView* MyEGLView::sharedOpenGLView()
{
	if (s_pMainWindow == NULL)
	{
		s_pMainWindow = new MyEGLView();
	}
	return s_pMainWindow;
}

bool MyEGLView::Create(LPCTSTR pTitle, int w, int h)
{
	return true;
}

// bool MyEGLView::initGL()
// {
// 	return true;
// }
// 
// void MyEGLView::destroyGL()
// {
// 
// }